;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var databind = require("databind");
	var c = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var html_create;
	var html_createMember = require("./memberGrade-createGrade.html");
	var html_delete;
	var imgSrc;
	var memberGradeId;
	var creditLine;
	var interestRate;
	var gradeName;
	var defaultInterest;
	var remark;
	var dialogCss = require("css_dialog");
	var tools = require("tools");
	var init = function() {
		inquireSystemParamByName();
		initEvent();
		router.init();
		inquireMemberGradePageList();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		$("#add_mg_btn").unbind("click").click(function() {
			$("#popdialog").html(html_createMember);
			tools.requireInput($("#add_member_dialog"));
			dialog.showDialog($("#add_member_dialog"));

			$("#add_member_dialog").on("change", "#file", function() {
				uploadFile($("#file"), $(".addfile"), $("#evaluationTemplate"), "file");
			});

			$("#add_member_dialog").on("click", ".removeFile", function() {
				$(this).parents(".showFiles").remove();
				var fs = [];
				$.each($('#add_member_dialog .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#evaluationTemplate").val(fs);
			});

			$("#add_member_dialog").on("click", ".sure_yes", function() {
				databind.submitDom($("#add_member_dialog"), function() {
					base.alert(1, "新增会员等级成功！");
					dialog.hideDialog($("#add_member_dialog"));
					router.route("memberGrade/memberGrade.html");
				});
			});
			$("#add_member_dialog").on("click", ".sure_no", function() {
				dialog.hideDialog($("#add_member_dialog"));
			});
		});
		
		$("#memberGrade_tbody").on("click", ".memberGradeBtn1", function() {
			memberGradeId = $(this).parent().parent().parent().attr("memberGradeId");
			$("#popdialog").html(html_createMember);
			$("#add_member_dialog").attr("action", "updateMemberGrade");
			$("#add_member_dialog .dialogTitle").html("修改会员等级");
			tools.requireInput($("#add_member_dialog"));
			databind.fillInput($("#add_member_dialog"), {
				action: "inquireMemberGradeDetail.json?memberGradeId=" + memberGradeId,
				async: false
			});
			databind.fillForm($("#add_member_dialog"), {
				memberGradeId: memberGradeId
			});
			
			databind.disableInput($("#add_member_dialog"));
			$("#add_member_dialog .addfile").remove();
			$("#add_member_dialog .sure_no").remove();
			dialog.showDialog($("#add_member_dialog"));
			var fs = $("#evaluationTemplate").val().split(',');
			$.each(fs, function() {
				var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + this + '" class="showProductIntro">评估模板</a></span>';
				$("#add_member_dialog .addfile").before(str);
			});
			
			$("#add_member_dialog").on("click", ".sure_yes", function() {
				dialog.hideDialog($("#add_member_dialog"));
			});
		});

		$("#memberGrade_tbody").on("click", ".memberGradeBtn", function() {
			memberGradeId = $(this).parent().parent().parent().attr("memberGradeId");
			$("#popdialog").html(html_createMember);
			$("#add_member_dialog").attr("action", "updateMemberGrade");
			$("#add_member_dialog .dialogTitle").html("修改会员等级");
			tools.requireInput($("#add_member_dialog"));
			databind.fillInput($("#add_member_dialog"), {
				action: "inquireMemberGradeDetail.json?memberGradeId=" + memberGradeId,
				async: false
			});
			databind.fillForm($("#add_member_dialog"), {
				memberGradeId: memberGradeId
			});
			dialog.showDialog($("#add_member_dialog"));
			var fs = $("#evaluationTemplate").val().split(',');
			$.each(fs, function() {
				var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + this + '" class="showProductIntro">评估模板</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></span>';
				$("#add_member_dialog .addfile").before(str);
			});

			$("#add_member_dialog").on("click", ".removeFile", function() {
				$(this).parents(".showFiles").remove();
				var fs = [];
				$.each($('#add_member_dialog .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#evaluationTemplate").val(fs);
			});

			$("#add_member_dialog").on("change", "#file", function() {
				uploadFile($("#file"), $(".addfile"), $("#evaluationTemplate"), "file");
			});

			$("#add_member_dialog").on("click", ".sure_yes", function() {
				databind.submitDom($("#add_member_dialog"), function() {
					base.alert(1, "修改会员等级成功！");
					dialog.hideDialog($("#add_member_dialog"));
					router.route("memberGrade/memberGrade.html");
				});
			});

			$("#add_member_dialog").on("click", ".sure_no", function() {
				dialog.hideDialog($("#add_member_dialog"));
			});
		});

		$("#memberGrade_tbody").on("click", ".memberGradeDelBtn", function() {
			memberGradeId = $(this).parent().parent().parent().attr("memberGradeId");
			dialog.showConfirmDialog("删除会员等级", "确定要删除此会员等级记录？", function() {
				disableMemberGrade(memberGradeId);
				base.alert(1, "删除成功！");
				router.route("memberGrade/memberGrade.html");
			});
		});
	};
	
	//权限控制
	var inquireCurrentOperationList = function() {
		base.fnAjax({
			action: 'inquireCurrentOperationList.json',
			async: false,
			fnSuccess: function(data) {
				if (data.status == 200) {
					for (var i = 0; i < data.datas.length; i++) {
						var permission = data.datas[i];
						var permissionId = permission.permissionId;
						$.each($("#memberGrade .permissionLink"), function() {
							var $this = $(this);
							if ($this.attr("permissionId") == permissionId) {
								$this.removeClass("none");
							}
						});
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//查询列表
	var inquireMemberGradePageList = function() {
		var keyword = $("#keyword").val();
		base.fnAjax({
			action: "inquireMemberGradePageList.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				sessionUuid: base.getData("sid"),
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency)
					var tpl = $("#memberGrade_data_tpl").html();
					var html = juicer(tpl, data);
					$("#memberGrade_tbody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#memberGrade_pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireMemberGradePageList();
						});
					inquireCurrentOperationList();
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	//查询详情
	var inquireMemberGradeDetail = function(memberGradeId) {
		base.fnAjax({
			action: "inquireMemberGradeDetail.json?memberGradeId=" + memberGradeId,
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				sessionUuid: base.getData("sid"),
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$("#gradeName1").attr("value", data.datas[0].gradeName);
					$("#interestRate").val(data.datas[0].interestRate);
					$("#creditLine").val(data.datas[0].creditLine);
					$("#defaultInterest").val(data.datas[0].defaultInterest);
					$("#defaultInterest").val(data.datas[0].defaultInterest);
					$("#ad_describe").val(data.datas[0].remark);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					totalRecordCount,
					data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
						};
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	//删除
	var disableMemberGrade = function(memberGradeId) {
		base.fnAjax({
			action: "disableMemberGrade.json?",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				sessionUuid: base.getData("sid"),
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize,
				memberGradeId: memberGradeId,
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					totalRecordCount,
					data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
						};
					router.route("memberGrade/memberGrade.html");
				} else {
					base.alert(2, "删除失败");
				}
			}
		});
	};
	//通过参数名查询系统参数值
	var inquireSystemParamByName = function() {
		base.fnAjax({
			action: "inquireSystemParamByName.json?",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				sessionUuid: base.getData("sid"),
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize,
				paramName: "超期罚息费率"
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					defaultInterest = data.datas[0].paramValue;
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					totalRecordCount,
					data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireSystemParamByName();
						};
				} else {
					base.alert(2, "失败");
				}
			}
		});
	};

	//上传文件
	var uploadFile = function(dom, labelDom, valueDom, fileElementId) {
		if ($(dom).val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: fileElementId,
				dataType: 'json',
				success: function(data) {
					var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></span>';
					$(labelDom).before(str);
					var fs = [];
					$.each($('#add_member_dialog .mas'), function() {
						fs.push($(this).attr("fileUuid"));
					});
					$(valueDom).val(fs);
				},
				complete: function(xml) {
					$(dom).change(function() {
						uploadFile(dom, labelDom, valueDom, fileElementId);
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};
	return {
		init: init
	};
});